/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.InfoLogin;
import Model.AdminProcess;
import Model.MD5;
import Model.TechnicianProcess;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author h10t93
 */
public class ManageServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        AdminProcess ap = new AdminProcess();
        TechnicianProcess tp = new TechnicianProcess();
        if (action.equals("login")) {
            MD5 md5 = new MD5();
            String user = request.getParameter("txtuser");
            String pass = request.getParameter("txtpass");
            //response.sendRedirect("adminHome.jsp");
            String result = ap.adminLogin(user, pass);
            if (result.equals("admin")) {
                session.setAttribute("list", ap.fillterComp());
                response.sendRedirect("adminHome.jsp");
            } else if (result.equals("staff")) {
                InfoLogin info = (InfoLogin) ap.getInfoLogin(user).elementAt(0);
                session.setAttribute("nameTechnician", info.getName());
                session.setAttribute("IDTechnician", info.getId());
                session.setAttribute("staffViewListComp", tp.getComp(info.getId()));
                response.sendRedirect("technicianHome.jsp");
            } else {
                response.sendRedirect("managerLogin.jsp");
            }
        }
        //load home admin
        if (action.equals("load")) {
            session.setAttribute("list", ap.fillterComp());
            response.sendRedirect("adminHome.jsp");
        }
        if (action.equals("assignment")) {
            session.setAttribute("ass", ap.staffDivision());
            int id_comp = Integer.parseInt(request.getParameter("idComp"));
            if (session.getAttribute("idComp") != null) {
                request.getSession().removeAttribute("idComp");
            }
            session.setAttribute("idComp", id_comp);


            response.sendRedirect("adminAssignment.jsp");

        }
        if (action.equals("insertWord")) {
            int idStaff = Integer.parseInt(request.getParameter("idStaff"));
            String pri = request.getParameter("pri");
            int idComplaint = Integer.parseInt(session.getAttribute("idComp").toString());
            if (ap.devision(idStaff, idComplaint, pri)) {
                // response.sendError(1,"insert thanh cong");
                request.getSession().removeAttribute("list");
                session.setAttribute("list", ap.fillterComp());
                response.sendRedirect("adminHome.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        }
        if (action.equals("reportDetail")) {
            session.setAttribute("sReport", ap.getReportDetail());
            response.sendRedirect("adminReportDetail.jsp");
        }
        //report by date time
        // bao cao chi thiet theo ngay thang nam
        if (action.equals("reportDetailByDate")) {
            String date = request.getParameter("txtdate");
            session.setAttribute("sReport", ap.getReportDetailByDate(date));
            response.sendRedirect("adminReportDetail.jsp");
        }
        if (action.equals("reportDetailByMorth")) {
            String morth = request.getParameter("txt_morth_detail");
            String year = request.getParameter("txt_year_detail");
            session.setAttribute("sdetailByMorth", ap.getReportDetailByMorth(morth, year));
            response.sendRedirect("adminReportDetailByMorth.jsp");
        }
        if (action.equals("reportDetailByYear")) {
            String year = request.getParameter("txt_year_detail1");
            session.setAttribute("sReport", ap.getReportDetailByYear(year));
            response.sendRedirect("adminReportDetailByYear.jsp");
        }
//report by pending complaints
        if (action.equals("pendingComp")) {
            session.setAttribute("ListPendingComp", ap.getPendingComp());

            session.setAttribute("totalCompPending", ap.getTotalCompPending());
            response.sendRedirect("adminViewPendingComp.jsp");
        }
        //report by pending complaints by date
        if (action.equals("reportPendingCompByDate")) {
            String date1 = request.getParameter("txtdateP");
            session.setAttribute("ListPendingComp", ap.getPendingCompByDate(date1));
            response.sendRedirect("adminViewPendingComp.jsp");
        }
        if (action.equals("reportPendingCompByMorth")) {
            String morth = request.getParameter("txtMorth");
            String year = request.getParameter("txtYear");
            session.setAttribute("totalCompPendingByMorth", ap.getTotalCompPendingByMorth(morth, year));
            session.setAttribute("listPCByMorth", ap.getPendingCompByMorth(morth, year));

            response.sendRedirect("adminViewPendingCompByMorth.jsp");
        }
        if (action.equals("reportPendingCompByYear")) {
            String year = request.getParameter("txtyear");
            session.setAttribute("listPCByYear", ap.getPendingCompByYear(year));
            session.setAttribute("totalCompPendingByYear", ap.getTotalCompPendingByYear(year));

            response.sendRedirect("adminViewPendingCompByYear.jsp");
        }
        if (action.equals("selectToResolve")) {
            int idW = Integer.parseInt(request.getParameter("idWordResolved").toString());
            int idC = Integer.parseInt(request.getParameter("idCompResolve").toString());
            session.setAttribute("sIdW", idW);
            session.setAttribute("sIdC", idC);
            response.sendRedirect("technicianResolve.jsp");
        }
        if (action.equals("resolve")) {
            int idW = Integer.parseInt(session.getAttribute("sIdW").toString());
            int idC = Integer.parseInt(session.getAttribute("sIdC").toString());
            String solutin = request.getParameter("txtSolutin");
            if (tp.resolveComp(idW, solutin, idC)) {
                int idStaff = Integer.parseInt(session.getAttribute("IDTechnician").toString());
                session.setAttribute("staffViewListComp", idStaff);
                response.sendRedirect("technicianHome.jsp");
                response.sendRedirect("technicianHome.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        }
    }
//staff resolve complaints 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
